<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
        <ArticleItem
          v-for="(obj,index) in list"
          :key="index"
          @disLikeEV="disLikeFn"
          :artObj="obj"
          @reportEV="reportFn"
          @click.native="itemClickFn(obj.art_id)"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { getAllArticleList, dislikeArticleAPI, reportArticleAPI } from '@/api'
import Notify from '@/ui/Notify.js'

export default {
  props: {
    channelId: Number // 文章列表的id
  },
  data() {
    return {
      loading: false,
      finished: false,
      list: [],
      theTime: new Date().getTime(),
      isLoading: false // 顶部加载状态
    }
  },
  created() {
    this.getArticleListFn()
  },
  methods: {
    async getArticleListFn() {
      const res = await getAllArticleList({
        channel_id: this.channelId,
        timestamp: this.theTime
      })
      console.log(res)
      this.list = [...this.list, ...res.data.data.results]
      this.theTime = res.data.data.pre_timestamp
      this.loading = false
      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
      this.isLoading = false
    },
    // 底部加载事件方法
    async onLoad() {
      this.getArticleListFn()
      // 返回最后完成判断
    },
    async onRefresh() {
      this.list = []
      this.theTime = new Date().getTime()
      this.getArticleListFn()
    },
    async disLikeFn(id) {
      await dislikeArticleAPI({ artId: id })
      Notify({ type: 'success', message: '反馈成功' })
    },
    async reportFn(id, val) {
      await reportArticleAPI({
        artId: id,
        type: val
      })
      Notify({ type: 'success', message: '举报成功' })
    },
    itemClickFn(id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }

  },
  components: {
    ArticleItem
  }
}
</script>

<style lang="less" scoped></style>
